class ResourceUtil{
    private static _instance:ResourceUtil;

    public static instance(){
        if(!this._instance)
        {
            this._instance = new ResourceUtil();
        }
        return this._instance;
    }

    //解析单个奖励
    public parseSignleAward(award:string)
    {
        let awardArr = award.split('_');
        if(awardArr.length >= 3)
        {
            let itemId = parseInt(awardArr[1]);
            let itemData = DataJson.item_define[itemId];
            if(!itemData){
                LogUtil.log('error award:'+award);
                return null;
            }
            let itemIcon = itemData.icon;
            let desc = '';
            let itemNum = parseInt(awardArr[2]);
            if(itemData.type == 3)
            {
                desc = StringUtil.getSwfLangStrVar(5+'', [itemData.name+'']);
            }
            else
            {
                desc = StringUtil.getSwfLangStrVar(itemId+'', [itemNum+'']);
            }
            
            let awardData = {
                itemId:itemId,
                itemData:itemData,
                itemIcon:itemIcon,
                itemNum:itemNum,
                desc:desc
            }
            return awardData;
        }
        else
        {
            LogUtil.log('error award:'+award);
        }
        return null;
    }

    public addResource(award:string)
    {
        let awardArr = award.split('_');
        if(awardArr.length >= 3)
        {
            let itemId = parseInt(awardArr[1]);
            let itemNum = parseInt(awardArr[2]);
            switch(itemId)
            {
                case resourceType.gold:
                    RoleCache.instance().myMoney += itemNum;
                break;
                case resourceType.diamond:
                    RoleCache.instance().myDiamond += itemNum;
                break;
                case resourceType.killDragon:
                    GameManager.instance.addCurDragonTimes(itemNum);
                break;
                case resourceType.three:
                    GameManager.instance.addCurThreeTimes(itemNum);
                break;
                default:
                break;
            }
            GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGE);
        }
        else
        {
            LogUtil.log('error award:'+award);
        }
    }

    //解析签到奖励
    public parseSignAward(award:string, day:number):SignAward
    {
        let awardArr = award.split('_');
        if(awardArr.length >= 3)
        {
            let itemId = parseInt(awardArr[1]);
            let itemNum = parseInt(awardArr[2]);
            let itemData = DataJson.item_define[itemId];
            if(!itemData){
                LogUtil.error('error award:'+award);
                return null;
            }
            
            let awardData = {
                icon :'',
                desc :'',
                num  :itemNum
            }
            awardData.desc = itemData.name;
            if(itemId == resourceType.gold)
            {
                if(day < 3)
                {
                    awardData.icon = itemData.icon;
                }
                else
                {
                    awardData.icon = 'basket_new_4';
                }
            }
            else if(itemId == resourceType.diamond)
            {
                if(day < 3)
                {
                    awardData.icon = itemData.icon;
                }
                else
                {
                    awardData.icon = 'basket_new_5';
                }
            }
            else if(itemId == resourceType.killDragon || itemId == resourceType.three)
            {
                awardData.icon = itemData.icon;
            }
            else
            {
                awardData.icon = itemData.icon;
                awardData.num = 0;
                awardData.desc = '惊喜礼包';
            }
            
            return awardData;
        }
        else
        {
            LogUtil.log('error award:'+award);
        }
        return null;
    }
}

interface SignAward{
    icon:string,
    desc:string,
    num:number
}

enum resourceType{
    none,
    gold,
    diamond,
    killDragon, 
    three
}